package vader.util.htmltowxml4j.core;


import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * jinshuan.li 2020-03-01
 **/
public class UrlUtils {


  public static String https(String url) {

    if (StringUtils.isEmpty(url)) {
      return url;
    }
    if (StringUtils.startsWith(url, "//")) {
      return "https:" + url;
    }
    return url;
  }

  public static Map<String, String> parseParams(String url) {

    if (StringUtils.isEmpty(url)) {
      return new HashMap<>();
    }

    String paramString = StringUtils.substringAfter(url, "?");

    if (StringUtils.isEmpty(paramString)) {
      return new HashMap<>();
    }
    String[] split = paramString.split("&");

    Map<String, String> result = new HashMap<>();
    for (String s : split) {
      String[] split1 = s.split("=");
      if (split1 == null || split1.length != 2) {
        continue;
      }
      result.put(split1[0], split1[1]);
    }

    return result;

  }

}
